﻿var patientVM_Doctor = {
    Patient: ko.observableArray([]),
    Visits: ko.observableArray([]),
    //VisitsPage: ko.observableArray([]),
    VisitsPage: ko.observable(0),
    VisitsPageCount: ko.observable(0),
    VisitsPageNavHandling: function (indexController) {
        var self = this;
        if (self.VisitsPage() > 1) {
            indexController.enableVisitPreviousGroup(true);
        }
        if (self.VisitsPage() < self.VisitsPageCount()) {

            indexController.enableVisitNextGroup(true);
        }
    },
    Indications: ko.observableArray([]),
    IndicationsPage: ko.observable(0),
    IndicationsPageCount: ko.observable(0),
    IndicationsPageNavHandling: function (indexController) {
        var self = this;
        if (self.IndicationsPage() > 1) {
            indexController.enableIndicationPreviousGroup(true);
        }
        if (self.IndicationsPage() < self.IndicationsPageCount()) {

            indexController.enableIndicationNextGroup(true);
        }
    },
    //IndicationsPage: ko.observableArray([]),
    sortVisitsDESC: function(){
        this.Visits.sort(function (left, right) { return left.VisitID == right.VisitID ? 0 : (left.VisitID < right.VisitID ? 1 : -1) });
    },
    sortIndicationsDESC: function () {
        this.Indications.sort(function (left, right) { return left.IndicationID == right.IndicationID ? 0 : (left.IndicationID < right.IndicationID ? 1 : -1) });
    },

    loadPatientInfo: function (searchID, searchType, indexController) {
        var self = this;
        //stop current request
        if(self.loadVisitsPageRe!=null)
            self.loadVisitsPageRe.abort();
        if(self.loadIndicationsPageRe!=null)
            self.loadIndicationsPageRe.abort();
        //stop handling
        indexController.enableListIndication(false);
        indexController.enableListVisit(false);
        //indexController.enableBtnSearch(false);
        indexController.enableSearch(false);
        indexController.enableVisitPreviousGroup(false);
        indexController.enableVisitNextGroup(false);
        indexController.enableIndicationPreviousGroup(false);
        indexController.enableIndicationNextGroup(false);

        indexController.message.processing("processing");
        //remove old data
        self.Patient.removeAll();
        self.Visits.removeAll();
        self.VisitsPage(0);
        self.VisitsPageCount(0);
        self.Indications.removeAll();
        self.IndicationsPage(0);
        self.IndicationsPageCount(0);
        //Ajax Call Get Patient Info
        $.ajax({
            type: 'post',
            url: home + 'Doctor/GetPatientInfo',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ id: searchID, type: searchType }),
            success: function (data) {
                self.Patient(data[0]);
                self.Visits(data[1]);
                //alert(JSON.stringify(data));
                //self.VisitsPage(data[2]);
                //self.sortVisitsDESC();
                self.VisitsPage(data[2].PageNumber);
                self.VisitsPageCount(data[2].PageCount);
                self.Indications(data[3]);
                //self.IndicationsPage(data[4]);
                //self.sortIndicationsDESC();
                self.IndicationsPage(data[4].PageNumber);
                self.IndicationsPageCount(data[4].PageCount);
                //start handling
                //if (self.Patient()[0].ID > 0) {//have patient
                //alert(self.Patient()[0].ID);
                //if server return json object-have patient
                    indexController.enableListIndication(true);
                    indexController.enableListVisit(true);
                //}
                self.VisitsPageNavHandling(indexController);
                self.IndicationsPageNavHandling(indexController);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {//if server return string-success but no matched data
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
                //indexController.enableBtnSearch(true);
                indexController.enableSearch(true);
                indexController.searchTerm.focus();
            }
        });
    },
    loadVisitsPageRe: null,
    loadVisitsPage: function (indexController, pageNumber) {
        var self = this;
        //stop handling
        indexController.enableListVisit(false);
        indexController.enableVisitPreviousGroup(false);
        indexController.enableVisitNextGroup(false);

        indexController.message.processing("processing");
        //remove old data
        self.Visits.removeAll();
        //Ajax Call Get Patient Info
        self.loadVisitsPageRe = $.ajax({
            type: 'post',
            url: home + 'Doctor/GetVisitsPage',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ PatientID: self.Patient()[0].ID, RowsPerPage: 10, PageNumber: pageNumber }),
            success: function (data) {
                self.Visits(data[0]);
                self.VisitsPage(data[1].PageNumber);
                self.VisitsPageCount(data[1].PageCount);
                self.VisitsPageNavHandling(indexController);
                indexController.enableListVisit(true);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {//if server return string-success but no matched data
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
            }
        });
    },
    loadIndicationsPageRe: null,
    loadIndicationsPage: function (indexController, pageNumber) {
        var self = this;
        //stop handling
        indexController.enableListIndication(false);
        indexController.enableIndicationPreviousGroup(false);
        indexController.enableIndicationNextGroup(false);

        indexController.message.processing("processing");
        //remove old data
        self.Indications.removeAll();
        //Ajax Call Get Patient Info
        self.loadIndicationsPageRe = $.ajax({
            type: 'post',
            url: home + 'Doctor/GetIndicationsPage',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: JSON.stringify({ PatientID: self.Patient()[0].ID, RowsPerPage: 10, PageNumber: pageNumber }),
            success: function (data) {
                self.Indications(data[0]);
                self.IndicationsPage(data[1].PageNumber);
                self.IndicationsPageCount(data[1].PageCount);
                self.IndicationsPageNavHandling(indexController);
                indexController.enableListIndication(true);
                indexController.message.processComplete("process_complete");
            },
            error: function (err) {
                if (err.status == 200) {//if server return string-success but no matched data
                    messageController.processComplete(err.responseText);
                }
                else {
                    messageController.processComplete("process_error");
                }
            },
            complete: function (xhr, textStatus) {
            }
        });
    }
};
var VisitsPageInfo = ko.computed(function () {
    return "Page: " + patientVM_Doctor.VisitsPage() + "/" + patientVM_Doctor.VisitsPageCount();
});

var IndicationsPageInfo = ko.computed(function () {
    return "Page: " + patientVM_Doctor.IndicationsPage() + "/" + patientVM_Doctor.IndicationsPageCount();
});